package net.sf.odinms.exttools.doclet.pages;

import java.io.IOException;
import net.sf.odinms.exttools.doclet.AbstractPageWriter;
import net.sf.odinms.exttools.doclet.Doclet;

/**
 * Creates "index.html", a valid XHTML 1.0 Frameset for the main page. Prints
 * the frame details as well as a warning for browsers not supporting frames.
 */
public final class Frameset extends AbstractPageWriter {
	/**
	 * Generate the structural frameset for the Javadocs.
	 */
	public static void generateFrameset() {
		String filename = "index" + CONF.ext;
		try {
			new Frameset(filename);
		}
		catch (IOException exc) {
			throw Doclet.exception(exc, filename);
		}
	}
	
	/**
	 * Initialize and create the structural XHTML frameset page.
	 * 
	 * @param filename The desired name of the file (with extension).
	 * @throws IOException If {@link java.io.FileOutputStream} creation fails.
	 */
	private Frameset(String filename) throws IOException {
		super(filename);
		// Generate all header information for an XHTML 1.0 Frameset
		println(DTD_XHTML1_FRAMES);
		println(HTML_XML);
		println(open("head"));
		String windowTitle =
				(CONF.windowtitle.length() > 0)
						? CONF.windowtitle
						: CONF.propertyText("doclet.Generated_Docs_Untitled");
		println(open("title") + windowTitle + close("title"));
		printTimeStamp();
		if (CONF.charset.length() > 0) {
			TagBuilder meta = new TagBuilder("meta");
			meta.add("http-equiv", "Content-Type");
			meta.add("content", "text/html");
			meta.add("charset", CONF.charset);
			println(meta.getEmpty());
		} else
			println(META_UTF8);
		// Script to set the classFrame if necessary.
		println(scriptOpenJavascript());
		println("<!--");
		println("    targetPage = \"\" + window.location.search;");
		println("    if (targetPage != \"\" && targetPage != \"undefined\")");
		println("       targetPage = targetPage.substring(1);");
		println("    function loadFrames() {");
		println("        if (targetPage != \"\" && targetPage != \"undefined\")");
		println("             top.classFrame.location = top.targetPage;");
		println("    }");
		println("//-->");
		println(close("script"));
		println(close("head"));
		
		// Generate <frameset> and <frame> sections depending on # of packages
		// Print outer frame regardless of number of packages (split L-R)
		println(new TagBuilder("frameset").add("cols", "20%,80%").add("title",
				"").add("onload", "top.loadFrames()").getOpen());
		if (CONF.packages.length <= 1) {
			// Print only tag for all classes (omit package frame in upper left)
			printFrameNameSrcTitle("packageFrame", "allclasses-frame"
					+ CONF.ext, CONF.propertyText("AllClasses"));
		} else {
			// For 2 or more packages, split left frame into 2 frames (split
			// T-B)
			println(new TagBuilder("frameset").add("rows", "30%,70%").add(
					"title", "").add("onload", "top.loadFrames()").getOpen());
			printFrameNameSrcTitle("packageListFrame", "overview-frame"
					+ CONF.ext, CONF.propertyText("AllPackages"));
			printFrameNameSrcTitle("packageFrame", "allclasses-frame"
					+ CONF.ext, CONF.propertyText("AllClasses"));
			println(close("frameset"));
		}
		printFrameNameSrcTitle("classFrame", CONF.topFile,
				CONF.propertyText("AllDescriptions"));

		printNoFramesSection(); // For browsers that don't support frames.
		
		println(close("frameset"));
		println(close("html"));
		this.close();
	}
	
	/**
	 * Print a <code>&lt;noframes&gt;</code> section for older browsers.
	 */
	private void printNoFramesSection() {
		println(open("noframes"));
		println(open("body"));
		// Try to redirect non-frames browsers to the correct page with
		// JavaScript
		println(scriptOpenJavascript());
		println("<!--");
		println("	window.location = \"" + CONF.topFile + "\";");
		println("//-->");
		println(close("script"));
		// Print a warning for browsers that don't support frames and w/o
		// JavaScript
		println(open("noscript"));
		println(open("h2") + CONF.propertyText("doclet.Frame_Alert")
				+ close("h2"));
		println(open("p") + CONF.propertyText("doclet.Frame_Warning_Message")
				+ close("p"));
		print(open("p") + CONF.propertyText("doclet.Link_To") + " ");
		print(new TagBuilder("a").add("href", CONF.topFile).getOpenTextClose(
				CONF.propertyText("doclet.Non_Frame_Version")));
		println(close("p"));
		println(close("noscript"));
		// Close this section
		println(close("body"));
		println(close("noframes"));
	}
	
	/**
	 * Convenience method for printing a &lt;frame&gt; tag with given
	 * parameters.
	 * 
	 * @param name The name for this frame, referenced by child pages, etc.
	 * @param src The initial source file that should occupy this frame.
	 * @param title The human-readable title to give this frame.
	 */
	private void printFrameNameSrcTitle(String name, String src, String title) {
		println(new TagBuilder("frame").add("name", name).add("src", src).add(
				"title", title).getEmpty());
	}
	
}
